// Class to represent a row in the sections grid
function Section(data) {
    var self = this;
    self.id = data.id;
    self.name = ko.observable(data.name).extend({required: true});
    self.weight = ko.observable(data.weight);
    self.fields = ko.observableArray();
    self.deletedFields = ko.observableArray();

    // Operations
    self.updateFieldPosition = function() {
        var i = 0;
        ko.utils.arrayForEach(self.fields(), function(field) {
            field.weight(i++);
        });
    }
    self.addField = function(data, event) {

        var allowOptions = false;
        var availableWidgets = [];
        var availableValidations = [];
        var widget_type;

        switch (data) {
            case '1':
                availableWidgets = [{value: '1', text: Yii.t('noseyModule.app', 'Textfield')}, {value: '2', text: Yii.t('noseyModule.app', 'Text Area')}];
                widget_type = '1';
                availableValidations = [{value: 'required', text: Yii.t('noseyModule.app', 'Required')}, {value: 'numerical', text: Yii.t('noseyModule.app', 'Numerical')}, {value: 'digits', text: Yii.t('noseyModule.app', 'Digits')}, {value: 'email', text: Yii.t('noseyModule.app', 'Email')}, {value: 'url', text: Yii.t('noseyModule.app', 'URL')}];
                break;
            case '2':
                availableWidgets = [{value: '3', text: Yii.t('noseyModule.app', 'Radio Buttons')}, {value: '5', text: Yii.t('noseyModule.app', 'Dropdown')}];
                availableValidations = [{value: 'required', text: Yii.t('noseyModule.app', 'Required')}];
                widget_type = '3';
                allowOptions = true;
                break;
            case '3':
                availableWidgets = [{value: '4', text: Yii.t('noseyModule.app', 'Checkboxes')}, {value: '5', text: Yii.t('noseyModule.app', 'Dropdown')}];
                availableValidations = [{value: 'required', text: Yii.t('noseyModule.app', 'Required')}];
                widget_type = '4';
                allowOptions = true;
                break;
            case '4':
            case '5':
                availableValidations = [{value: 'required', text: Yii.t('noseyModule.app', 'Required')}];
                break;
            case '6':
                availableWidgets = [{value: '6', text: Yii.t('noseyModule.app', 'Star Rating')}, {value: '5', text: Yii.t('noseyModule.app', 'Dropdown')}];
                availableValidations = [{value: 'required', text: Yii.t('noseyModule.app', 'Required')}];
                widget_type = '6';
                allowOptions = true;
                break;
            default:
                break;
        }
        var Fila = new Field({allowOptions: allowOptions, field_type: data, availableWidgets: availableWidgets, widget_type: widget_type, availableValidations: availableValidations})
        self.fields.push(Fila);
        if (data == 6)
        {
            Fila.addOption();
            ocultarRating();
            validacionCampoMaximoRating();
        }

        self.updateFieldPosition();
        $('textarea').autosize();
    }
    self.removeField = function(field) {
        bootbox.confirm(Yii.t('noseyModule.app', 'Do you really want to delete this field?'), function(confirmed) {
            if (confirmed) {
                self.fields.remove(field);
                if (field.id)
                    self.deletedFields.push(field);
                self.updateFieldPosition();
            }
        })
    }
}